<?php
/**
 * 微信接口
 */
namespace App\Http\Controllers\API;

use App\Ext\Wechat\Push;
use App\Http\Controllers\Controller;

class WechatController extends Controller
{
    public function index()
    {
        $push = new Push();
        $push->valid('123');
        $type = $push->receive()->getType();
        switch ($type) {
            # 文本消息
            case $push::MSG_TEXT:
                $push->text('text')->reply();
                break;
            # 图片消息
            case $push::MSG_IMAGE:
                $push->text('image')->reply();
                break;
            # 语音消息
            case $push::MSG_VOICE:
                $push->text('voice')->reply();
                break;
            # 视频消息
            case $push::MSG_VIDEO:
                $push->text('video')->reply();
                break;
            # 小视频消息
            case $push::MSG_SHORTVIDEO:
                $push->text('shortvideo')->reply();
                break;
            # 地理位置消息
            case $push::MSG_LOCATION:
                $push->text('location: ' . json_encode($push->getLocation()))->reply();
                break;
            # 链接消息
            case $push::MSG_LINK:
                $push->text('link')->reply();
                break;
            # 事件消息
            case $push::MSG_EVENT:
            $event = $push->getEvent();
            switch ($event['event']) {
                # 关注事件
                case $push::EVENT_SUBSCRIBE:
                    $openid = $push->getFrom();
                    # ToDo Something
                    # 没有绑定会员账号的，向用户推送绑定会员账号消息
                    /*
                    if (empty($userWeChat->user_id)) {
                        $param = [
                            [
                                'Title' => '您还没有绑定账号',
                                'Description' => '点击图片绑定账号',
                                'PicUrl' => 'http://www.xxx.com/wechat_bind.jpg',
                                'Url' => $this->config['url'] . '/wechat/user/bind'
                            ]
                        ];
                        $push->news($param)->reply(); # 没绑定手机的推送绑定页
                    }
                    */
                    $push->text('subscribe: openid=' . $openid . ' qrscene=' . $event['eventKey'])->reply();
                    break;
                # 取消关注事件
                case $push::EVENT_UNSUBSCRIBE:
                    $openid = $push->getFrom();
                    # ToDo Something
                    exit(); # 直接回复空串（指字节长度为0的空字符串，而不是XML结构体中content字段的内容为空）
                    $push->text()->reply(); # 取消关注
                    break;
                # 扫描带参数二维码事件
                case $push::EVENT_SCAN:
                    $openid = $push->getFrom();
                    # 这里是扫描的是“检测是否关注&是否绑定二维码”，如果已经关注，是走这个事件，可以在这里检测是否绑定手机号。
                    # ToDo Something
                    # 没有绑定会员账号的，向用户推送绑定会员账号消息
                    /*
                    if (empty($userWeChat->user_id)) {
                        $param = [
                            [
                                'Title' => '您还没有绑定账号',
                                'Description' => '点击图片绑定账号',
                                'PicUrl' => 'http://www.xxx.com/wechat_bind.jpg',
                                'Url' => $this->config['url'] . '/wechat/user/bind'
                            ]
                        ];
                        $push->news($param)->reply(); # 没绑定手机的推送绑定页
                    }
                    */
                    $push->text('subscribe: openid=' . $openid . ' qrscene=' . $event['eventKey'])->reply();
                    break;
                # 上报地理位置事件
                case $push::EVENT_LOCATION:
                    $push->text('LOCATION: ' . json_encode($push->eventLocation()))->reply();
                    break;
            }
            # 默认消息
            default:
                $push->text('default')->reply();
                break;
        }
    }
}
